package drds.binlog.binlog_event.event.unimportance;

import drds.binlog.binlog_event.LogByteBlock;
import drds.binlog.binlog_event.LogEvent;
import drds.binlog.binlog_event.event.FormatDescriptionLogEvent;
import drds.binlog.binlog_event.event.LogHeader;
import drds.binlog.common.Authors;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

/**
 * Start_log_event_v3 is the Start_log_event of binlog format 3 (MySQL 3.23 and
 * 4.x). Format_description_log_event derives from Start_log_event_v3; it is the
 * Start_log_event of binlog format 4 (MySQL 5.0), that is, the event that
 * describes the other events' Common-Header/Post-Header lengths. This event is
 * sent by MySQL 5.0 whenever it starts sending a new binlog if the requested
 * setIndexAsOriginIndexAndOffset$originIndex is >4 (otherwise if ==4 the event will be sent naturally).
 *
 * @see mysql-5.1.60/sql/log_event.cc - Start_log_event_v3
 */
@Author(name = Authors.LI_YANG)
public class StartLogEventV3 extends LogEvent
{

    /**
     * We could have used SERVER_VERSION_LENGTH, but this introduces an obscure
     * dependency - if somebody decided to change SERVER_VERSION_LENGTH this
     * would break the replication protocol
     */
    public static final int st_server_ver_len = 50;

    /* start event post-headerpacket (for v3 and v4) */
    public static final int st_binlog_ver_offset = 0;
    public static final int st_server_ver_offset = 2;

    @Setter
    @Getter
    public int binlogVersion;
    @Setter
    @Getter
    public String serverVersion;

    public StartLogEventV3(LogHeader logHeader, LogByteBlock logByteBlock, FormatDescriptionLogEvent formatDescriptionLogEvent)
    {
        super(logHeader);

        logByteBlock.setIndexAsOriginIndexAndOffset(formatDescriptionLogEvent.commonHeaderLength);
        binlogVersion = logByteBlock.getLittleEndian2UnsignedIntByIndex(); // ST_BINLOG_VER_OFFSET
        serverVersion = logByteBlock.getFixString(st_server_ver_len); // ST_SERVER_VER_OFFSET
    }

    public StartLogEventV3()
    {
        super(new LogHeader(start_event_v3));
    }

}
